package com.usian.cars.service.impl;


import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.cars.feign.OrderFeign;
import com.usian.cars.mapper.CarBrandsMapper;
import com.usian.cars.mapper.CarImagesMapper;
import com.usian.cars.mapper.CarsMapper;
import com.usian.cars.service.ICarsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.Result;
import com.usian.entity.CarBrands;
import com.usian.entity.CarImages;
import com.usian.entity.Cars;
import com.usian.entity.Favorites;
import com.usian.vo.CarsVo;
import com.usian.vo.CarsVo2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.usian.vo.CarsReserveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@Service
public class CarsServiceImpl extends ServiceImpl<CarsMapper, Cars> implements ICarsService {


    @Autowired
    private CarImagesMapper carImagesMapper;


    @Override
    public List<CarsVo> getCarAllByBrandId(Integer brandId) {
        // 创建一个 LambdaQueryWrapper 对象用于查询汽车品牌信息
        LambdaQueryWrapper<Cars> carBrandsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件为品牌 ID 等于传入的品牌 ID
        carBrandsLambdaQueryWrapper.eq(Cars::getBrandId, brandId);
        // 执行查询，获取汽车列表
        List<Cars> list = list(carBrandsLambdaQueryWrapper);
        System.out.println("汽车" + list);
        // 获取汽车图片信息
        List<CarImages> carImages = getCarImages(list);
        // 创建一个 ArrayList 用于存储汽车视图对象
        ArrayList<CarsVo> carsVos = new ArrayList<>();
        // 遍历汽车列表，将汽车对象的属性复制到视图对象，并添加到视图对象列表
        for (Cars cars : list) {
            CarsVo carsVo = new CarsVo();
            BeanUtils.copyProperties(cars, carsVo);
            carsVos.add(carsVo);
        }
        // 遍历汽车图片列表，将图片 URL 添加到对应的汽车视图对象
        for (CarImages carImage : carImages) {
            for (CarsVo carsVo1 : carsVos) {
                if (carImage.getCarId().equals(carsVo1.getCarId())) {
                    carsVo1.setImageUrl(Collections.singletonList(carImage.getImageUrl()));
                }
            }
        }
        System.out.println("carsVos" + carsVos);
        // 返回汽车视图对象列表
        return carsVos;
    }

    @Override
    public List<CarsVo> findAll() {
        List<Cars> list = list();
        ArrayList<CarsVo> carsVos = new ArrayList<>();
        // 遍历汽车列表，将汽车对象的属性复制到视图对象，并添加到视图对象列表
        for (Cars cars : list) {
            CarsVo carsVo = new CarsVo();
            BeanUtils.copyProperties(cars, carsVo);
            carsVos.add(carsVo);
        }
        // 获取汽车图片信息
        List<CarImages> carImages = getCarImages(list);
        // 遍历汽车图片列表，将图片 URL 添加到对应的汽车视图对象
        for (CarImages carImage : carImages) {
            for (CarsVo carsVo1 : carsVos) {
                if (carImage.getCarId().equals(carsVo1.getCarId())) {
                    carsVo1.setImageUrl(Collections.singletonList(carImage.getImageUrl()));
                }
            }
        }
        return carsVos;
    }

    @Override
    public CarsVo getByIdCarVo(Integer carId) {
        // 创建一个Lambda查询包装器对象，用于后续的条件查询
        LambdaQueryWrapper<Cars> carsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件，根据汽车ID查询
        carsLambdaQueryWrapper.eq(Cars::getCarId, carId);
        // 执行查询，获取一条汽车记录
        Cars cars = getOne(carsLambdaQueryWrapper);
        // 检查查询结果是否为空，如果为空则返回null
        if (cars == null) {
            return null;
        }
        // 创建一个汽车视图对象，用于封装查询结果
        CarsVo carsVo = new CarsVo();
        // 将汽车对象的属性复制到汽车视图对象中
        BeanUtils.copyProperties(cars, carsVo);
        // 获取汽车图片列表
        List<CarImages> carImages = getCarImages(Collections.singletonList(cars));
        // 遍历汽车图片列表，寻找与当前汽车匹配的图片
        for (CarImages carImage : carImages) {
            // 检查当前图片是否属于当前汽车
            if (carImage.getCarId().equals(carsVo.getCarId())) {
                // 如果匹配，则将图片URL设置到汽车视图对象中
                carsVo.setImageUrl(Collections.singletonList(carImage.getImageUrl()));
            }
        }
        // 返回封装好的汽车视图对象
        return carsVo;
    }

    @Override
    public List<CarsVo> getCarsByCarModel(Cars cars) {
        List<CarsVo2> carsByCarModel = this.baseMapper.getCarsByCarModel(cars);
        ArrayList<CarsVo> carsVos = new ArrayList<>();
        for (CarsVo2 carsVo2 : carsByCarModel) {
            CarsVo carsVo = new CarsVo();
            BeanUtils.copyProperties(carsVo2,carsVo);
            carsVo.setImageUrl(Collections.singletonList(carsVo2.getImageUrl()));
            carsVos.add(carsVo);
        }
        return carsVos;
    }

    @Autowired
    private OrderFeign orderFeign;

    @Override
    public Boolean updateCar(CarsVo cars) {
        System.out.println("cars" + cars);
        if (cars.getIsFavorite()==1){
            Favorites favorites = new Favorites();
            favorites.setUserId(cars.getUserId());
            favorites.setCarId(cars.getCarId());
            orderFeign.addFavorites(favorites);
        }else {
            orderFeign.deleteFavorites(cars.getUserId(),cars.getCarId());
        }
        Cars cars1 = new Cars();
        BeanUtils.copyProperties(cars,cars1);
        return updateById(cars1);
    }


    // 提取的公共方法，根据汽车列表获取汽车图片信息
    private List<CarImages> getCarImages(List<Cars> carsList) {
        // 创建一个 ArrayList 存储汽车 ID
        List<Integer> carIdList = carsList.stream().map(Cars::getCarId).collect(Collectors.toList());
        // 创建一个 LambdaQueryWrapper 对象用于查询汽车图片信息
        LambdaQueryWrapper<CarImages> carImagesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 遍历汽车列表，设置查询条件为汽车 ID 等于列表中的汽车 ID
        carImagesLambdaQueryWrapper.in(CarImages::getCarId, carIdList);
        // 执行查询，获取汽车图片列表
        return carImagesMapper.selectList(carImagesLambdaQueryWrapper);
    }

    @Autowired
    private CarsMapper carsMapper;
    @Autowired
    private CarImagesServiceImpl carImagesService;

    @Override
    public List<CarsReserveVo> findScreen(String brand, Integer price, String character) {
        List<CarsReserveVo> carsReserveVoList = carsMapper.findScreen(brand, price, character);
        if (carsReserveVoList.size() == 0){
            return null;
        }
        //循环carsReserveVoList获取汽车的id
        for (CarsReserveVo carsReserveVo : carsReserveVoList) {
            Integer carId = carsReserveVo.getCarId();
            //根据id获取汽车的图片
            String carImg = carImagesService.getCarImagesByCarId(carId).getData();
            carsReserveVo.setImageUrl(carImg);

        }
        return carsReserveVoList;
    }

    @Override
    public Boolean updateFavorites() {
        LambdaQueryWrapper<Cars> carsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carsLambdaQueryWrapper.eq(Cars::getIsFavorite,1);
        List<Cars> cars = list(carsLambdaQueryWrapper);
        for (Cars car : cars) {
            car.setIsFavorite(0);
            updateById(car);
        }
        return true;
    }

    @Override
    public Boolean updateFavoritesAll(Integer carId) {
        LambdaQueryWrapper<Cars> carsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carsLambdaQueryWrapper.eq(Cars::getCarId,carId);
        Cars cars = getOne(carsLambdaQueryWrapper);
        if (cars.getIsFavorite()==0){
            cars.setIsFavorite(1);
            updateById(cars);
            return true;
        }
        return true;
    }
}
